/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package doblepaso;

import processing.core.PApplet;

/**
 *
 * @author DAVINCI
 */
public class DoblePaso extends PApplet {

    int xStart;
    int yStart;
    int xEnd;
    int yEnd;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        PApplet.main(new String[]{"doblepaso.DoblePaso"});
    }

    @Override
    public void setup() {
        size(800, 600);
        background(200);

    }

    @Override
    public void draw() {
    }

    public void DoblePaso(int xStart, int yStart, int xEnd, int yEnd) {
        int dx = xEnd - xStart;
        int dy = yEnd - yStart;
        int d, c;
        int x = xStart;
        int y = yStart;
        int incr1;
        int incr2 = 4 * dy - 2 * dx;
        //pendiente menor que 1/2
        if (incr2 < 0) {
            c = 2 * dy;
            incr1 = 2 * c;
            d = incr1 - dx;
            point(x, y);
            while(x <= xEnd) {

                if (d < 0) {// patron 1
                    x++;
                    point(x, y);
                    x++;
                    point(x, y);
                    d = d + incr1;
                } else {
                    if (d < c) {//patron 2
                        x++;
                        point(x, y);
                        x++;
                        y++;
                        point(x, y);
                    } else {//patron 3
                        x++;
                        y++;
                        point(x, y);
                        x++;
                        point(x, y);
                    }
                    d = d + incr2;
                }
            }
        } else {// pendiente >= 1/2
            c = 2 * (dy - dx);
            incr1 = 2 * c;
            d = incr1 + dx;
            while (x <= xEnd) {
                if (d > 0) {// patron 4
                    x++;
                    y++;
                    point(x, y);
                    x++;
                    y++;
                    point(x, y);
                    d = d + incr1;
                } else {
                    if (d < c) {//patron 2
                        x++;
                        point(x, y);
                        x++;
                        y++;
                        point(x, y);
                    } else {//patron 3
                        x++;
                        y++;
                        point(x, y);
                        x++;
                        point(x, y);
                    }
                    d = d + incr2;
                }
            }
        }


    }

    @Override
    public void mousePressed() {
        xStart = mouseX;
        yStart = mouseY;

    }

    @Override
    public void mouseDragged() {
        background(200);
        xEnd = mouseX;
        yEnd = mouseY;
        DoblePaso(xStart, yStart, xEnd, yEnd);
//        BresenhamLine(5, 8,90,90);
    }
}
